import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import terser from '@rollup/plugin-terser';
import serve from 'rollup-plugin-serve';//搭建本地服务器
import livereload from 'rollup-plugin-livereload';//监听文件变化，自动刷新浏览器

const port = 8000;
// eslint-disable-next-line no-undef
const env = process.env.NODE_ENV;
const devPlugins = () => {
    return [
        serve({
            port,
            open: true,
            openPage: '/debug/index.html'
        }),
        livereload({
            port,
            watch: ['dist', 'debug']
        })
    ]
};
const previewPlugins = () => {
    return [
        serve({
            port,
            open: true,
            openPage: '/docs/index.html'
        }),
        livereload({
            port,
            watch: ['dist', 'docs']
        })
    ]
}
const nodeEnvFunc = {
    'development': devPlugins(),
    'preview': previewPlugins()
}

export default [
    {
        input: './src/index.js',
        output: {
            dir: 'dist',
            format: 'cjs',
            entryFileNames: '[name].cjs.js',
        },
        plugins: [
            resolve(),
            commonjs(),
            terser(),
            ...(nodeEnvFunc[env] || [])
        ],
    },
    {
        input: './src/index.js',
        output: {
            dir: 'dist',
            format: 'esm',
            entryFileNames: '[name].esm.js',
        },
        plugins: [
            resolve(),
            commonjs(),
            terser()
        ],
    }
];